package cn.tedu.carehome.foundation.repo.impl;

import cn.tedu.carehome.foundation.pojo.vo.VisitorListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.VisitorStandardVO;
import cn.tedu.carehome.foundation.repo.IVisitorRedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Repository
public class VisitorRedisRepositoryImpl implements IVisitorRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Override
    public void save(VisitorStandardVO visitorStandardVO) {
        String key=BRAND_ITEM_KEY_PREFIX+visitorStandardVO.getId();
        ValueOperations<String,Serializable> opsForValue=redisTemplate.opsForValue();
        opsForValue.set(key,visitorStandardVO);
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(BRAND_ALL_KEYS_KEY,key);
    }

    @Override
    public void save(List<VisitorListItemVO> visitorListItemVOS) {
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        for (VisitorListItemVO item:visitorListItemVOS){
            opsForList.rightPush(BRAND_LIST_KEY,item);
        }
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(BRAND_ALL_KEYS_KEY,BRAND_LIST_KEY);
    }

    @Override
    public void deleteAll() {
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        Set<Serializable> serializableSet=opsForSet.members(BRAND_ALL_KEYS_KEY);
        Set<String> keys=new HashSet<>();
        for (Serializable serializable:serializableSet){
            keys.add((String) serializable);
        }
        redisTemplate.delete(keys);
        redisTemplate.delete(BRAND_ALL_KEYS_KEY);
    }

    @Override
    public VisitorStandardVO get(Long id) {
       String key=BRAND_ITEM_KEY_PREFIX+id;
        ValueOperations<String, Serializable> opsForValue = redisTemplate.opsForValue();
        Serializable serializable= opsForValue.get(key);
        if (serializable!=null){
            return (VisitorStandardVO) serializable;
        }
        return null;
    }

    public List<VisitorListItemVO> list(){
        return list(0,-1);
    }

    @Override
    public List<VisitorListItemVO> list(long start, long end) {
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        List<Serializable> list=opsForList.range(BRAND_LIST_KEY,start,end);
        List<VisitorListItemVO> visitorListItemVOList=new ArrayList<>();
        for (Serializable serializable:list){
            visitorListItemVOList.add((VisitorListItemVO) serializable);
        }
        return visitorListItemVOList;
    }
}
